import store from '../store'
import db from './db.js'
import common from './common.js'

//公共拦截器
let response = function(res){
	return res;
}
var networkType = ''
uni.getNetworkType({
    success: function (res) {
		networkType = res.networkType
    }
});
export default{
	post(url,data,status){
		if(status != true) {
			uni.showLoading({
				title: '加载中'
			});
		}
		return uni.request({
			url: store.state.baseUrl + url,
			data: data,
			method:'POST',
			dataType :'json',
			header: {
				token: db.get('token')
			}
		}).then(data =>{
			if(status != true) {
				uni.hideLoading();
			}
			const [error,res] = data;
			if(!res){
				console.log('请求超时')
				uni.showToast({title:'当前网络错误，请检查网络',icon:'none'})
				uni.hideLoading();
				return 
			}else if(res.data.statusCode == 401){
				// #ifdef MP-WEIXIN
				setTimeout(()=>{
					common.judgeLogin()
				},1000)
				// #endif
				// #ifdef APP-PLUS || H5
				setTimeout(()=>{
					common.judgeLogin()
				},1000)
				// #endif
			}else if(res.data.state == false){
				let msg = res.data.data.message
				uni.showToast({icon:"none",title:msg});
			}
			return response(res.data);
		})
	},
	get(url,data,status){
		if(status != true) {
			uni.showLoading({
				title: '加载中'
			});
		}
		return uni.request({
			url: store.state.baseUrl + url,
			data: data,
			method:'GET',
			header: {
				token: db.get('token')
			}
		}).then(dataRes =>{
			if(status != true) {
				uni.hideLoading();
			}
			const [error,res] = dataRes;
			if(!res){
				console.log('请求超时')
				uni.showToast({title:'当前网络错误，请检查网络',icon:'none'})
				uni.hideLoading();
				return 
			}else if(res.data.statusCode == 401){
				// #ifdef MP-WEIXIN
				setTimeout(()=>{
					common.judgeLogin()
				},1000)
				// #endif
				// #ifdef APP-PLUS || H5
				setTimeout(()=>{
					common.judgeLogin()
				},1000)
				// #endif
			}else if(res.data.state == false){
				let msg = res.data.data.message
				uni.showToast({icon:"none",title:msg});
			}
			return response(res.data);
		})
	}
}